package com.glorysoft.lipuxinpda.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glorysoft.lipuxinpda.R;
import com.glorysoft.lipuxinpda.adapter.RealTimeRecyclerViewAdapter;
import com.glorysoft.lipuxinpda.base.BaseActivity;
import com.glorysoft.lipuxinpda.base.Constant;
import com.glorysoft.lipuxinpda.entity.BaseEntity;
import com.glorysoft.lipuxinpda.entity.MessageInfoEntity;
import com.glorysoft.lipuxinpda.entity.RealTimeMessageReturnEntity;
import com.glorysoft.lipuxinpda.utils.GsonUtils;
import com.glorysoft.lipuxinpda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

public class RealTimeActivity extends BaseActivity {

    private LinearLayout llMain;

    private TextView tv_eqp_no;//设备号

    private RecyclerView rv_realtime_message;//实时消息

    private RealTimeRecyclerViewAdapter mRealTimeRecyclerViewAdapter;//实时消息适配器

    private String sEqpID = "";//设备号


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_real_time;
    }

    @Override
    protected void initView() {

        tv_eqp_no = findViewById(R.id.tv_eqp_no);

        rv_realtime_message = findViewById(R.id.rv_realtime_message);

        llMain = findViewById(R.id.llMain);
    }

    @Override
    protected void initData() {
        Bundle extras = getIntent().getExtras();
        sEqpID = extras.getString("EQPID");

        tv_eqp_no.setText(sEqpID);
        getRealTimeMessage();//获取实时信息
        //region 测试
//        String data = "{\n" +
//                "     \"data\":{\n" +
//                "        \"EQPID\":\"2FS101\",\n" +
//                "        \"MessageList\":[\n" +
//                "            {\n" +
//                "                \"CREATED_TIME\":\"2020/10/11 14:11:11\",\n" +
//                "                \"OPERAND\":\"PDA\",\n" +
//                "                \"OPERATION_CONTENT\":\"点击批次开始按钮\",\n" +
//                "                \"MSG_ID\":\"1111\"\n" +
//                "            },\n" +
//                "            {\n" +
//                "                \"CREATED_TIME\":\"2020/10/11 14:21:11\",\n" +
//                "                \"OPERAND\":\"PDA\",\n" +
//                "                \"OPERATION_CONTENT\":\"设备开始作业\",\n" +
//                "                \"MSG_ID\":\"1121\"\n" +
//                "             }]}}";
//        try {
//            JSONObject jsonObject = new JSONObject(data);
//            String sData = jsonObject.getString("data");
//            RealTimeMessageReturnEntity body = GsonUtils.getObject(sData, RealTimeMessageReturnEntity.class);
//            showRealTimeMessage(body);//显示实时消息
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        //endregion
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle(getString(R.string.RealTimeMessage));
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    //region 接口调用

    //获取实时消息
    private void getRealTimeMessage() {
        FreeUI_EntityCallBack<BaseEntity<RealTimeMessageReturnEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<RealTimeMessageReturnEntity>>(new TypeToken<BaseEntity<RealTimeMessageReturnEntity>>() {
        }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseEntity<RealTimeMessageReturnEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getSupportFragmentManager())) {
                    showToast(response.body().getMessage(), true);

                    RealTimeMessageReturnEntity body = response.body().getData().getBody();
                    showRealTimeMessage(body);//显示实时消息
                }
            }

            @Override
            public void onError(Response<BaseEntity<RealTimeMessageReturnEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "RealTimeMessage");
            }
        };

        HashMap<String, String> map = new HashMap<String, String>();

        map.put("EQP_ID", sEqpID);//设备号
        String method = Constant.INSTANCE.getNET_URL() + "/RealTimeMessage";
        new NetUtils().requestPostNetNews(method, this, map, callBack);
    }

    //endregion

    //显示实时消息
    private void showRealTimeMessage(RealTimeMessageReturnEntity entity) {
        if (entity != null){
            List<MessageInfoEntity> messageList = entity.getMessageList();
            mRealTimeRecyclerViewAdapter = new RealTimeRecyclerViewAdapter();

            rv_realtime_message.setLayoutManager(new LinearLayoutManager(getBaseContext(), LinearLayoutManager.VERTICAL, false));
            rv_realtime_message.setAdapter(mRealTimeRecyclerViewAdapter);
            mRealTimeRecyclerViewAdapter.setmList(messageList);
        }
    }
}